﻿using DeZhen.Core.Framework.Entity;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace DeZhen.Core.Framework.Http
{
    [Service]
    public class HttpClient : IHttpClient
    {
        public String Post(String url, String json, Dictionary<String, String> heads = null)
        {
            try
            {
                HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(url);
                webrequest.Method = "post";
                webrequest.ContentType = "application/json;charset=utf-8";
                webrequest.Timeout = 120000;
                webrequest.KeepAlive = false;
                System.Net.ServicePointManager.DefaultConnectionLimit = 512;
                if (heads != null)
                {
                    foreach (var head in heads)
                    {
                        webrequest.Headers.Remove(head.Key);
                        webrequest.Headers.Add(head.Key, head.Value);
                    }
                }
                byte[] postdatabyte = Encoding.UTF8.GetBytes(json);
                webrequest.ContentLength = postdatabyte.Length;
                Stream stream;
                stream = webrequest.GetRequestStream();
                stream.Write(postdatabyte, 0, postdatabyte.Length);
                stream.Close();
                using (var httpWebResponse = webrequest.GetResponse())
                using (StreamReader responseStream = new StreamReader(httpWebResponse.GetResponseStream()))
                {
                    String ret = responseStream.ReadToEnd();
                    string result = ret.ToString();
                    return result;
                }
            }
            catch (WebException ex)
            {
                var response = (HttpWebResponse)ex.Response;
                Stream myResponseStream = ex.Response.GetResponseStream();
                using (StreamReader responseStream = new StreamReader(myResponseStream))
                {
                    String ret = responseStream.ReadToEnd();
                    string result = ret.ToString();
                    return result;
                }
            }

        }
        public async Task<String> PostAsync(String url, String json, Dictionary<String, String> heads = null)
        {
            try
            {
                HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(url);
                webrequest.Method = "post";
                webrequest.ContentType = "application/json;charset=utf-8";
                webrequest.Timeout = 120000;
                webrequest.KeepAlive = false;
                System.Net.ServicePointManager.DefaultConnectionLimit = 512;
                if (heads != null)
                {
                    foreach (var head in heads)
                    {
                        webrequest.Headers.Remove(head.Key);
                        webrequest.Headers.Add(head.Key, head.Value);
                    }
                }
                byte[] postdatabyte = Encoding.UTF8.GetBytes(json);
                webrequest.ContentLength = postdatabyte.Length;
                Stream stream;
                stream = await webrequest.GetRequestStreamAsync();
                stream.Write(postdatabyte, 0, postdatabyte.Length);
                stream.Close();
                using (var httpWebResponse = await webrequest.GetResponseAsync())
                using (StreamReader responseStream = new StreamReader(httpWebResponse.GetResponseStream()))
                {
                    String ret = await responseStream.ReadToEndAsync();
                    string result = ret.ToString();
                    return result;
                }
            }
            catch (WebException ex)
            {
                var response = (HttpWebResponse)ex.Response;
                Stream myResponseStream = ex.Response.GetResponseStream();
                using (StreamReader responseStream = new StreamReader(myResponseStream))
                {
                    String ret = responseStream.ReadToEnd();
                    string result = ret.ToString();
                    return result;
                }
            }

        }
        public String Get(String url, Dictionary<String, String> heads = null)
        {
            try
            {
                HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(url);
                webrequest.Timeout = 120000;
                webrequest.KeepAlive = false;
                System.Net.ServicePointManager.DefaultConnectionLimit = 512;
                if (heads != null)
                {
                    foreach (var head in heads)
                    {
                        webrequest.Headers.Remove(head.Key);
                        webrequest.Headers.Add(head.Key, head.Value);
                    }
                }
                webrequest.Method = "get";
                using (var httpWebResponse = webrequest.GetResponse())
                using (StreamReader responseStream = new StreamReader(httpWebResponse.GetResponseStream()))
                {
                    String ret = responseStream.ReadToEnd();
                    string result = ret.ToString();
                    return result;
                }
            }
            catch (WebException ex)
            {
                var response = (HttpWebResponse)ex.Response;
                Stream myResponseStream = ex.Response.GetResponseStream();
                using (StreamReader responseStream = new StreamReader(myResponseStream))
                {
                    String ret = responseStream.ReadToEnd();
                    string result = ret.ToString();
                    return result;
                }
            }

        }
        public async Task<String> GetAsync(String url, Dictionary<String, String> heads = null)
        {
            try
            {
                HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(url);
                webrequest.Timeout = 120000;
                webrequest.KeepAlive = false;
                System.Net.ServicePointManager.DefaultConnectionLimit = 512;
                if (heads != null)
                {
                    foreach (var head in heads)
                    {
                        webrequest.Headers.Remove(head.Key);
                        webrequest.Headers.Add(head.Key, head.Value);
                    }
                }
                webrequest.Method = "get";
                using (var httpWebResponse = await webrequest.GetResponseAsync())
                using (StreamReader responseStream = new StreamReader(httpWebResponse.GetResponseStream()))
                {
                    String ret = responseStream.ReadToEnd();
                    string result = ret.ToString();
                    return result;
                }
            }
            catch (WebException ex)
            {
                var response = (HttpWebResponse)ex.Response;
                Stream myResponseStream = ex.Response.GetResponseStream();
                using (StreamReader responseStream = new StreamReader(myResponseStream))
                {
                    String ret = responseStream.ReadToEnd();
                    string result = ret.ToString();
                    return result;
                }
            }

        }
    }
}
